var res2model = function(model, res){
	for (var i = 0; i < res.rows.length; ++i){
		model.push(res.rows.item(i))
	}
}
var outputerr = function(err){
	console.log(JSON.stringify(err));
	console.log(JSON.stringify(err.stack));
}



angular.module('starter.services', [])

.factory('SIMCHAPTER', function(){
	var select_chapters = {}
	return {
		getchapters: function(icourseid){
			if (select_chapters[icourseid]){
				return select_chapters[icourseid]
			} else {
				select_chapters[icourseid] = []
				return  select_chapters[icourseid]
			}
		},
		setchapters: function(icourseid, chapterids){
			select_chapters[icourseid] = chapterids
		}
	}
})
.factory('SOAP', function() {
  // Might use a resource here that returns a JSON array

  // Some fake testing data
  	//Object.prototype.setMGSoapHeader = function(){}
	var header = new tempuri_org__MGSoapHeader()
	header.setUserID("ttexam")
	header.setPassword("100exam")
	var service = new tempuri_org__ExamJsonServiceSoap()
	console.log(ionic.Platform.platform())
	if (ionic.Platform.platform() == 'ios' ||
		ionic.Platform.platform() == 'android'){
		// service.url = 'http://www.zx100t.com/CL.ExamWebService/ExamJsonService.asmx'
		service.url = 'http://121.41.120.234/CL.ExamWebService/ExamJsonService.asmx'
	} else {
		service.url = 'http://localhost:8100/CL.ExamWebService/ExamJsonService.asmx'
	}
	service.setHeader(header)

  	return service
})

.factory('USER', function(){
	var userinfo = {}
	return {
		userinfo: function(){return userinfo},
		setuserinfo: function(info){userinfo = info}
	}
})

.factory('LOGOUT', function($ionicPopup, $state){
	return {logoutnotify: function(event){
	      console.log(JSON.stringify(event))
	      console.log($state.current.name)
	      if($state.current.name=="app.info" || $state.current.name == "login"){
	        var downloading = $ionicPopup.show({
	          title: '确定要退出吗?',
	          buttons: [{text: '确定', type: 'button-assertive', onTap: function(){navigator.app.exitApp()}},
	                    {text: '取消', type: 'button-positive', onTap: function(){return}}
	          ]
	        })
	      }
	      else {
	        navigator.app.backHistory();
	      }
		}
	}
})

.factory('DB', function($cordovaSQLite){
	
	if (ionic.Platform.platform() == 'ios' ||
		ionic.Platform.platform() == 'android') {

		console.log("mobile platform, using sqlite")
		return {
			serverExchangeItem: function(){return serverExchangeItem;},
			classes: function(){
				var ret = []
				$cordovaSQLite.execute(db, "select * from courseclass",[]).then(
					function(res){res2model(ret, res);},outputerr);
				return ret;
			},
			
			getclass: function(classid){
				var ret = {}
				$cordovaSQLite.execute(db, "select * from courseclass where iclassid = ?",[classid]).then(
					function(res){ ret = res.rows.item(0)},outputerr);
				return ret;
			},
			getsubclasses: function(classid){
				var ret = []
				$cordovaSQLite.execute(db, "select * from coursesubclass where iclassid = ?",[classid]).then(
					function(res){res2model(ret, res)},outputerr);
				return ret;
			},
			getsubclass: function(subclassid){
				var ret = {};
				$cordovaSQLite.execute(db, "select * from coursesubclass where isubclassid = ?", [subclassid]).then(
					function(res){ret = res.rows.item(0)},outputerr);
				return ret;
			},
			getcourses: function(subclassid){
				var ret = [];
				$cordovaSQLite.execute(db, "select * from course where isubclassid = ?", [subclassid]).then(
					function(res){res2model(ret,res)}, outputerr);
				return ret;
			},
			getcourse: function(courseid){
				var ret = {};
				$cordovaSQLite.execute(db, "select * from course where icourseid = ?", [courseid]).then(
					function(res){ret = res.rows.item(0)}, outputerr);
				return ret;
			},
			chapters: function(){
				var ret = [];
				$cordovaSQLite.execute(db, "select * from coursechapter", []).then(
					function(res){res2model(ret, res)},outputerr);
				return ret;
			},
			getchapter: function(chapterid){
				var ret = {};
				$cordovaSQLite.execute(db, "select * from coursechapter where ichapterid = ?", [chapterid]).then(
					function(res){ret = res.rows.item(0)},outputerr);
				return ret;
			},
			updatechapters: function(cs){
				$cordovaSQLite.execute(db, "select * from coursechapter where ichapterid = ?", [cs.ichapterid]).then(
					function(res){
						//console.log("select " + JSON.stringify(res))
						if (res.rows.length == 0){
							$cordovaSQLite.execute(db, "insert into coursechapter " + 
								"(ballowtest, bendgrade, bstopflag, cchaptercode, cchaptername, cdcname, cmemo, dchangedate, " + 
								"ichapterid,icount, icourseid, iexamtime, igrade, itype ) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
								[cs.ballowtest, cs.bendgrade, cs.bstopflag, cs.cchaptercode, cs.cchaptername, cs.cdcname, cs.cmemo, cs.dchangedate, cs.ichapterid, cs.icount, cs.icourseid, cs.iexamtime, cs.igrade, cs.itype]).then(
								function(res){
									//console.log("result" + JSON.stringify(res))},function(res){"error" + console.log(JSON.stringify(res))
								})
						} else {
							$cordovaSQLite.execute(db, "update coursechapter set ballowtest = ?, bendgrade = ?, bstopflag = ?, cchaptercode = ?," +
								"cchaptername = ?, cdcname = ?, cmemo = ?, dchangedate = ?, ichapterid = ?, icount = ?," +
								"icourseid = ?, iexamtime = ?, igrade = ?, itype = ? where ichapterid = ?",
								[cs.ballowtest, cs.bendgrade, cs.bstopflag, cs.cchaptercode, cs.cchaptername, cs.cdcname, cs.cmemo, cs.dchangedate, cs.ichapterid, cs.icount, cs.icourseid, cs.iexamtime, cs.igrade, cs.itype,cs.ichapterid]).then(
								function(res){
									//console.log("result" + JSON.stringify(res))},function(res){"error" + console.log(JSON.stringify(res))
								})
						}
					},
					function(err){
						console.log("select err" + JSON.stringify(res))
					}
				);
			},
			getsubchapters: function(chapterinfo){
				var ret = [];
				if (typeof(chapterinfo) == 'number'){
					$cordovaSQLite.execute(db, "select * from coursechapter where icourseid = ? and igrade = 0",[chapterinfo]).then(
						function(res){res2model(ret,res);console.log(JSON.stringify(ret));},outputerr);
					console.log(JSON.stringify(ret))
				} else {
					$cordovaSQLite.execute(db, "select * from coursechapter where icourseid = ? and igrade = ?", [chapterinfo.icourseid, chapterinfo.igrade + 1]).then(
						function(res){res2model(ret,res)}, outputerr);
				}
				return ret;
			},
			getsubjects: function(chapterid){
				var ret = [];
				$cordovaSQLite.execute(db, "select * from coursesubject where ichapterid = ?", [chapterid]).then(
					function(res){res2model(ret,res)},outputerr)
				return ret;
			},
			updateCourse: function(courses){
				$.each(courses, function(i,course){
					$cordovaSQLite.execute(db, "select * from course where icourseid = ?", [course.icourseid]).then(function(res){
						if (res.rows.length == 0){ //insert
							$cordovaSQLite.execute(db, "insert into course (icourseid, iclassid, isubclassid, " + 
								"ccoursename, ballowtest, iindex, bstopflag) values(?,?,?,?,?,?,?)", 
								[course.icourseid, course.iclassid, course.isubclassid, course.ccoursename,course.ballowtest, course.iindex, course.bstopflag])
							//.then(function(r){console.log("insert course done")},outputerr)
						} else { //update
							$cordovaSQLite.execute(db, "update course set iclassid = ?, isubclassid = ?, ccoursename = ?, ballowtest = ?, iindex = ?, bstopflag = ? where icourseid = ?",
								[course.iclassid, course.isubclassid, course.ccoursename,course.ballowtest, course.iindex, course.bstopflag, course.icourseid])
							//.then(function(r){console.log("update course done")},outputerr)
						}
					},outputerr)
				})
			},
			updateCourseClass: function(courseclasses){
				$.each(courseclasses, function(i,courseclass){
					$cordovaSQLite.execute(db, "select * from courseclass where iclassid = ?", [courseclass.iclassid]).then(function(res){
						if (res.rows.length == 0){ //insert
							$cordovaSQLite.execute(db, "insert into courseclass (iclassid, ccoursecname, iindex, csubjecttypes) " + 
								"values(?,?,?,?)",
								[courseclass.iclassid, courseclass.ccoursecname, courseclass.iindex, courseclass.csubjecttypes])
							.then(function(r){console.log("insert courseclass done")},outputerr);
						} else { //update
							$cordovaSQLite.execute(db, "update courseclass set ccoursecname = ?, iindex = ?, csubjecttypes = ? where iclassid = ?",
								[courseclass.ccoursecname, courseclass.iindex, courseclass.csubjecttypes, courseclass.iclassid])
							.then(function(r){console.log("update courseclass done")},outputerr);
						}
					},outputerr)
				})
			},
			updateCourseSubclass: function(coursesubclasses){
				$.each(coursesubclasses, function(i,subclass){
					$cordovaSQLite.execute(db, "select * from course where isubclassid = ?", [subclass.isubclassid]).then(function(res){
						if (res.rows.length == 0){ //insert
							$cordovaSQLite.execute(db, "insert into coursesubclass (isubclassid, csubclassname, iclassid, iindex, bstopflag) " + 
								" values (?,?,?,?,?)",
								[subclass.isubclassid, subclass.csubclassname, subclass.iclassid, subclass.iindex, subclass.bstopflag])
							.then(function(r){console.log("insert coursesubclass done")},outputerr);
						} else { //update
							$cordovaSQLite.execute(db, "update coursesubclass set csubclassname = ?, iclassid = ?, iindex = ?, bstopflag = ? where isubclassid = ? ", 
								[subclass.csubclassname, subclass.iclassid, subclass.iindex, subclass.bstopflag, subclass.isubclassid])
							.then(function(r){console.log("update coursesubclass done")},outputerr);
						}
					},outputerr)
				})
			},
			updateSysExchangeItem: function(item){
				$cordovaSQLite.execute(db, "update sys_serverexchangeitem set dupdatedate = ? where id = ?", [item.dupdatedate, item.id])
				.then(function(r){console.log("sys_serverexchangeitem " + item.id + " updated")},outputerr)
			},

		}

	} else {
		console.log("web platform, using testing data")

		var serverExchangeItem = [
			{citemname: 'course', dupdatedate: Date('2014-09-29 17:03:28'), id : 3},
			{citemname: 'courseclass', dupdatedate: Date('2014-11-27 21:49:03'), id : 1},
			{citemname: 'coursesubclass', dupdatedate: Date('2014-09-29 09:56:35'), id : 2},
			{citemname: 'district', dupdatedate: Date('2014-09-29 17:03:28'), id : 8},
			{citemname: 'sys_cussetting', dupdatedate: Date('2014-11-26 17:24:33'), id : 4},
			{citemname: 'sys_knowledge', dupdatedate: Date('2013-04-15 01:01:27'), id : 6},
			{citemname: 'sys_message', dupdatedate: Date('2014-06-17 14:53:35'), id : 5},
			{citemname: 'sys_photoad', dupdatedate: Date('2012-12-29 00:55:24'), id : 7}
		]

		var classes = [
			{iclassid: 3, ccoursecname: '建造工程', dchangedate: '2014-11-27 21:49:03',  iindex: 1,  bstopflag: 0,  csubjecttypes: null},
			{iclassid: 6, ccoursecname: '财务会计', dchangedate: '2014-11-27 21:49:03',  iindex: 2,  bstopflag: 0,  csubjecttypes: null},
			{iclassid: 7, ccoursecname: '经济贸易', dchangedate: '2014-11-27 21:49:03',  iindex: 3,  bstopflag: 0,  csubjecttypes: null},
			{iclassid: 9, ccoursecname: '医药卫生', dchangedate: '2014-11-27 21:49:03',  iindex: 4,  bstopflag: 0,  csubjecttypes: null},
			{iclassid: 10, ccoursecname: '英语考试', dchangedate: '2014-11-27 21:49:03',  iindex: 5,  bstopflag: 0,  csubjecttypes: null},
			{iclassid: 11, ccoursecname: '从业资格', dchangedate: '2014-11-27 21:49:03',  iindex: 6,  bstopflag: 0,  csubjecttypes: null},
			{iclassid: 12, ccoursecname: '内部考前', dchangedate: '2014-11-27 21:49:03',  iindex: 7,  bstopflag: 0,  csubjecttypes: null}
		]
		var subclasses = [
			{isubclassid: 1, csubclassname: '一级建造师', iclassid: 3, iindex: 1, dchangedate: '2012-12-20 22:40:30', bstopflag: 0},
			{isubclassid: 3, csubclassname: '初级经济师', iclassid: 7, iindex: 3, dchangedate: '2012-12-20 22:40:30', bstopflag: 0},
			{isubclassid: 4, csubclassname: '二级建造师', iclassid: 3, iindex: 2, dchangedate: '2012-12-20 22:40:30', bstopflag: 0},
			{isubclassid: 5, csubclassname: '造价工程师', iclassid: 3, iindex: 3, dchangedate: '2012-12-20 22:40:30', bstopflag: 0},
			{isubclassid: 6, csubclassname: '监理工程师', iclassid: 3, iindex: 4, dchangedate: '2012-12-20 22:40:30', bstopflag: 0},
			{isubclassid: 7, csubclassname: '房地产估价师', iclassid: 3, iindex: 5, dchangedate: '2012-12-20 22:40:30', bstopflag: 0},
			{isubclassid: 8, csubclassname: '会计从业资格考试', iclassid: 6, iindex: 6, dchangedate: '2012-12-20 22:40:30', bstopflag: 0},
			{isubclassid: 9, csubclassname: '初级会计职称', iclassid: 6, iindex: 1, dchangedate: '2012-12-20 22:40:30', bstopflag: 0},
			{isubclassid: 10, csubclassname: '中级会计职称', iclassid: 6, iindex: 2, dchangedate: '2012-12-20 22:40:30', bstopflag: 0},
			{isubclassid: 11, csubclassname: '注册会计师', iclassid: 6, iindex: 3, dchangedate: '2012-12-20 22:40:30', bstopflag: 0},
			{isubclassid: 14, csubclassname: '注册税务师', iclassid: 6, iindex: 4, dchangedate: '2012-12-20 22:40:30', bstopflag: 0},
			{isubclassid: 16, csubclassname: '中级经济师', iclassid: 7, iindex: 2, dchangedate: '2012-12-20 22:40:30', bstopflag: 0},
			{isubclassid: 19, csubclassname: '执业医师', iclassid: 9, iindex: 1, dchangedate: '2012-12-20 22:40:30', bstopflag: 0},
			{isubclassid: 20, csubclassname: '执业药师', iclassid: 9, iindex: 2, dchangedate: '2012-12-20 22:40:30', bstopflag: 0},
			{isubclassid: 21, csubclassname: '证券从业资格', iclassid: 11, iindex: 2, dchangedate: '2012-12-20 22:40:30', bstopflag: 0},
			{isubclassid: 22, csubclassname: '职称英语', iclassid: 10, iindex: 1, dchangedate: '2012-12-20 22:40:30', bstopflag: 0},
			{isubclassid: 29, csubclassname: '期货从业资格', iclassid: 11, iindex: 3, dchangedate: '2012-12-20 22:40:30', bstopflag: 0},
			{isubclassid: 30, csubclassname: '银行从业资格', iclassid: 11, iindex: 4, dchangedate: '2012-12-20 22:40:30', bstopflag: 0},
			{isubclassid: 31, csubclassname: '司法考试', iclassid: 11, iindex: 1, dchangedate: '2012-12-20 22:40:30', bstopflag: 0},
			{isubclassid: 32, csubclassname: '教师资格', iclassid: 11, iindex: 5, dchangedate: '2012-12-20 22:40:30', bstopflag: 0},
		]
		var courses = [
			{icourseid: 4, ihadbuy: 0,isubclassid: 4, ccoursename: '二级建造师建筑专业实务'},
			{icourseid: 5, isubclassid: 4, ccoursename: '二级建造师机电专业实务'},
			{icourseid: 7, isubclassid: 1, ccoursename: '一级建造师建设工程法规及相关知识'},
			{icourseid: 9, isubclassid: 1, ccoursename: '一级建造师建设工程项目管理'},
			{icourseid: 10, isubclassid: 1, ccoursename: '一级建造师建设工程经济'},
			{icourseid: 12, isubclassid: 1, ccoursename: '一级建造师建设工程 管理与实务(市政工程)'},
			{icourseid: 13, isubclassid: 1, ccoursename: '一级建造师建设工程 管理与实务(建筑工程)'},
			{icourseid: 14, isubclassid: 1, ccoursename: '一级建造师建设工程 管理与实务(机电工程)'},
			{icourseid: 15, isubclassid: 4, ccoursename: '二级建造师建设施工管理'},
			{icourseid: 16, isubclassid: 4, ccoursename: '二级建造师法规与相关知识'},
			{icourseid: 17, isubclassid: 4, ccoursename: '二级建造师公路专业实务'},
			{icourseid: 18, isubclassid: 4, ccoursename: '二级建造师市政专业实务'},
			{icourseid: 20, isubclassid: 10, ccoursename: '中级财务管理'},
			{icourseid: 21, isubclassid: 10, ccoursename: '中级经济法'},
			{icourseid: 22, isubclassid: 10, ccoursename: '中级会计实务'},
			{icourseid: 24, isubclassid: 5, ccoursename: '建筑工程造价管理'},
			{icourseid: 25, isubclassid: 5, ccoursename: '工程造价计价与控制'},
			{icourseid: 26, isubclassid: 5, ccoursename: '工程造价案例分析'},
			{icourseid: 27, isubclassid: 5, ccoursename: '建设工程技术与计量(土建)'},
			{icourseid: 28, isubclassid: 45, ccoursename: '建设工程技术与计量(安装)'},

		]

		var chapters = [
			{ichapterid: 48, icourseid: 7, cchaptercode: '01', itype: 0, cchaptername: '章节练习(2014新版章节)', bendgrade: "FALSE", igrade: 0, ballowtest: "FALSE", icount: 930, tested_count: 5, right_precent: "1"},
			{ichapterid: 52, icourseid: 7, cchaptercode: '0101', itype: 0, cchaptername: '1Z301000建设工程基本法律知识', bendgrade: "FALSE", igrade: 1, ballowtest: "FALSE", icount: 930, tested_count: 5, right_precent: "1"},
			{ichapterid: 162, icourseid: 7, cchaptercode: '010101', itype: 0, cchaptername: '1Z301010建设工程法律体系', bendgrade: "TRUE", igrade: 2, ballowtest: "TRUE", icount: 930, tested_count: 5, right_precent: "1"},
			{ichapterid: 163, icourseid: 7, cchaptercode: '010102', itype: 0, cchaptername: '1Z301020建设工程法人制度', bendgrade: "TRUE", igrade: 2, ballowtest: "FALSE", icount: 930, tested_count: 5, right_precent: "1"},
			{ichapterid: 164, icourseid: 7, cchaptercode: '010103', itype: 0, cchaptername: '1Z301030建设工程代理制度', bendgrade: "TRUE", igrade: 2, ballowtest: "FALSE", icount: 930, tested_count: 5, right_precent: "1"},
			{ichapterid: 165, icourseid: 7, cchaptercode: '010104', itype: 0, cchaptername: '1Z301040建设工程物权制度', bendgrade: "TRUE", igrade: 2, ballowtest: "FALSE", icount: 930, tested_count: 5, right_precent: "1"},
			{ichapterid: 166, icourseid: 7, cchaptercode: '010105', itype: 0, cchaptername: '1Z301050建设工程债权制度', bendgrade: "TRUE", igrade: 2, ballowtest: "FALSE", icount: 930, tested_count: 5, right_precent: "1"},
			{ichapterid: 167, icourseid: 7, cchaptercode: '010106', itype: 0, cchaptername: '1Z301060建设工程知识产权制度', bendgrade: "TRUE", igrade: 2, ballowtest: "FALSE", icount: 930, tested_count: 5, right_precent: "1"},
			{ichapterid: 168, icourseid: 7, cchaptercode: '010107', itype: 0, cchaptername: '1Z301070建设工程担保制度', bendgrade: "TRUE", igrade: 2, ballowtest: "FALSE", icount: 930, tested_count: 5, right_precent: "1"},
			{ichapterid: 169, icourseid: 7, cchaptercode: '010108', itype: 0, cchaptername: '1Z301080建设工程保险制度', bendgrade: "TRUE", igrade: 2, ballowtest: "FALSE", icount: 930, tested_count: 5, right_precent: "1"},
			{ichapterid: 170, icourseid: 7, cchaptercode: '010109', itype: 0, cchaptername: '1Z301090建设工程法律责任制度', bendgrade: "TRUE", igrade: 2, ballowtest: "FALSE", icount: 930, tested_count: 5, right_precent: "1"},
		]
		var subjects = [
		{isubjectid: 1, ichapterid: 162, iindex: 1, isubjecttype: 0, iscore: 1, ctitle: '测试题', cquestion: "A.宪法至上\nB.新发优于旧法\nC.特别法优于一般法\nD.一般法优于特别法",
		 canswer: 'C', ianswercount: 4, cdescription: '因为什么什么什么,所以什么什么什么'},
 		{isubjectid: 1, ichapterid: 162, iindex: 2, isubjecttype: 0, iscore: 1, ctitle: '测试题', cquestion: "A.宪法至上\r\nB.新发优于旧法\r\nC.特别法优于一般法\r\nD.一般法优于特别法",
		 canswer: 'C', ianswercount: 4, cdescription: '因为什么什么什么,所以什么什么什么'},
 		{isubjectid: 1, ichapterid: 162, iindex: 3, isubjecttype: 0, iscore: 1, ctitle: '测试题', cquestion: "A.宪法至上\r\nB.新发优于旧法\r\nC.特别法优于一般法\r\nD.一般法优于特别法",
		 canswer: 'C', ianswercount: 4, cdescription: '因为什么什么什么,所以什么什么什么'},
 		{isubjectid: 1, ichapterid: 162, iindex: 4, isubjecttype: 0, iscore: 1, ctitle: '测试题', cquestion: "A.宪法至上\r\nB.新发优于旧法\r\nC.特别法优于一般法\r\nD.一般法优于特别法",
		 canswer: 'C', ianswercount: 4, cdescription: '因为什么什么什么,所以什么什么什么'},
 		{isubjectid: 1, ichapterid: 162, iindex: 5, isubjecttype: 0, iscore: 1, ctitle: '测试题', cquestion: "A.宪法至上\r\nB.新发优于旧法\r\nC.特别法优于一般法\r\nD.一般法优于特别法",
		 canswer: 'C', ianswercount: 4, cdescription: '因为什么什么什么,所以什么什么什么'},
 		{isubjectid: 1, ichapterid: 162, iindex: 6, isubjecttype: 0, iscore: 1, ctitle: '测试题', cquestion: "A.宪法至上\r\nB.新发优于旧法\r\nC.特别法优于一般法\r\nD.一般法优于特别法",
		 canswer: 'C', ianswercount: 4, cdescription: '因为什么什么什么,所以什么什么什么'},
 		{isubjectid: 1, ichapterid: 162, iindex: 7, isubjecttype: 0, iscore: 1, ctitle: '测试题', cquestion: "A.宪法至上\r\nB.新发优于旧法\r\nC.特别法优于一般法\r\nD.一般法优于特别法",
		 canswer: 'C', ianswercount: 4, cdescription: '因为什么什么什么,所以什么什么什么'},
 		{isubjectid: 1, ichapterid: 162, iindex: 8, isubjecttype: 0, iscore: 1, ctitle: '测试题', cquestion: "A.宪法至上\r\nB.新发优于旧法\r\nC.特别法优于一般法\r\nD.一般法优于特别法",
		 canswer: 'C', ianswercount: 4, cdescription: '因为什么什么什么,所以什么什么什么'},
		]
		return {
			serverExchangeItem: function(){return serverExchangeItem;},
			classes: function(){return classes;},
			getclass: function(classid){
				return $.grep(classes, function(element){if (element.iclassid == classid){return true;} else {return false;}})[0]
			},
			getsubclasses: function(classid){
				return $.grep(subclasses, function(element){if (element.iclassid == classid){return true;} else {return false;}})
			},
			getsubclass: function(subclassid){
				return $.grep(subclasses, function(element){ if (element.isubclassid == subclassid){return true;} else {return false;}})[0]
			},
			getcourses: function(subclassid){
				return $.grep(courses, function(element){ if (element.isubclassid == subclassid){return true;} else {return false;}})
			},
			getcourse: function(courseid){
				return $.grep(courses, function(element){ if (element.icourseid  == courseid){return true;} else{return false;}})[0]
			},
			chapters: function(){return chapters;},
			getchapter: function(chapterid){
				return $.grep(chapters, function(element){ if (element.ichapterid == chapterid){return true;} else {return false;}})[0]
			},
			getsubchapters: function(chapterinfo){
				if (typeof(chapterinfo) == 'number'){
					return $.grep(chapters, function(element){
						if (element.icourseid == chapterinfo && element.igrade ==0){
							return true;
						} else {
							return false;
						}
					})
				} else {
					return $.grep(chapters, function(element){
					 if (element.icourseid == chapterinfo.icourseid && element.igrade == (chapterinfo.igrade + 1)){
					 	return true;
					 } else {return false;}
					})
				}
			},
			getsubjects: function(chapterid){
				return $.grep(subjects, function(element){ if (element.ichapterid == chapterid){return true;} else {return false;}})
			},
			updatechapters: function(chapters){
				// console.log("db " + JSON.stringify(chapters))
			},
		}
	}
});